<?php

namespace App\Http\Controllers\Api\Payment;

use App;
use App\Http\Controllers\Controller;
use App\Jobs\Pay\ProcessWechatOrderPayCallback;
use App\Jobs\Pay\ProcessWechatRefundCallback;
use App\Models\Common\Channel\Channel;
use App\Trip\Wechat\Pay\PayRes\PayResult;
use App\Trip\Wechat\Pay\RefundRes\RefundResult;
use App\Trip\Wechat\Pay\WechatPay;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class PaymentNotifyController extends Controller
{
    /**
     */
    public function callback($pay_type, $channel_id, Request $request)
    {
//        $array = [
//            'id'            => '3cca29b3-2740-54d1-bd4a-c7a8a17b068d',
//            'create_time'   => '2023-04-25T12:03:13+08:00',
//            'resource_type' => 'encrypt-resource',
//            'event_type'    => 'TRANSACTION.SUCCESS',
//            'summary'       => '支付成功',
//            'resource'      => [
//
//                'original_type'   => 'transaction',
//                'algorithm'       => 'AEAD_AES_256_GCM',
//                'ciphertext'      => 'BYhqdKnMy838PpbvthXq7FXK09rRHTkjTBV00EEUsUaku/0Ai2CQ38tzjyqGmtRdRGze5iT3oakQ37M7KNcV0uDEhmOUVy6Ua0/g7NHfdsQyBo5G3BqNzryi1d36ckh1PBjM2xopmIO6+T0y4Lkl369/rcSt+Z09MX/2h6x1Z2RLOZmYltqILR4WKhRdO92oLSdGgTNEFJg5CIKK7kHkXcrl0mB8qliX0c1NkTYLs7UB5aRd2lqICa6Qq37JlBhrTSUY8FQ1DmbkybrN48v/voVVEF9P1vYvnEBG1qWg0nBS8dioNcPMRvvmJ4KF7WGV1NJGn+NUYcjRp5tVX1nibfD15lwBY/TOtVbll9seNrNrCn23i9CM4Ln5ULFbREr9qIDxgUOLzcE9F0dcWmCfxbB676Ki6Du6DjJmdiAZeVMQpn1qSMetqjk12qrgpegSzmDBBsdQR8fynTzaq8FxMN8vQCjbcfM2NZuZlt1JLkEnd9CsR4yBTNEPOSJ7X37UgVrj5tgp5ffEHHEbPdTa4FLbJLEr3ksqguxyjYOmd3lblZYVBeBQAQiI8dlQNfRuWTGnibCA+wbi8PMyNSPz3EyCsMXatNOGC4NwyYDTDYYHxCupLhq+1z6WS4NGO2w4DV/ubx0EP4ydO4mZQrEbH/E=',
//                'associated_data' => 'transaction',
//                'nonce'           => 'PkyhfLU8K344',
//            ],
//
//        ];
//        dd(json_encode($array,64|256));
        Log::info("pay callback before");
        Log::info($request->all());
        if (0 == $channel_id) {
            // TODO  rebuild like kaituanle
            Log::info("channel 0 , no biz refund pay callback after");
            return false;
        }
        if (App::isLocal()) {
            $channel_id = 2;
        }

        $channel   = Channel::whereChannelId($channel_id)->first();
        $wechatPay = new WechatPay(['appid' => $channel->getAppId()]);

        if ($pay_type == 'wechat') {
            $app = $wechatPay->getApplication();
            // $app 为你实例化的支付对象，此处省略实例化步骤
            $server = $app->getServer();
            // 处理支付结果事件
            $server->handlePaid(function ($message) {
                Log::info("handlePaid start");
                Log::info($message);
                if (App::isProduction()) {
                    ProcessWechatOrderPayCallback::dispatch(new PayResult($message))->onQueue('{pay_callback}');
                } else {
                    ProcessWechatOrderPayCallback::dispatchSync(new PayResult($message));
                }
                Log::info("handlePaid end");
            });

            // 处理退款结果事件
            $server->handleRefunded(function ($message) {
                Log::info("handleRefund start");
                Log::info($message);
                ProcessWechatRefundCallback::dispatch(new RefundResult($message))->onQueue('{pay_callback}');
                Log::info("handleRefund end");
            });

            return $server->serve();
        }
        Log::info("pay callback after");
    }
}
